package com.hexu.event.service;

import com.hexu.event.dao.ArticleDao;
import com.hexu.event.dao.UserDao;
import com.hexu.event.entity.Article;
import com.hexu.event.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class WorkerService {
    ArticleDao articleDao = new ArticleDao();
    UserDao userDao = new UserDao();

    //显示当前时间
    public static String dateDisplay() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    //增加文章
    public boolean addArticle(HttpServletRequest request, HttpServletResponse response){
        //调用请求对象读取请求头参数信息
        String name = request.getParameter("name");
        String content= request.getParameter("content");
        //调用请求对象，向tomcat索要当前用户在服务端私人储物柜
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Article article = new Article(null,name,content,0,0,0,"健康运动", user.getUserName(),0,dateDisplay());

        boolean flag = articleDao.addArticle(article);

        return flag;
    }

    //返回所有服务工作者
    public void getWorker(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User> userList = userDao.getWorker();
        request.setAttribute("worker",userList);
        request.getRequestDispatcher("/WorkerFind.jsp").forward(request, response);
    }

}
